<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    function ListNode(val, next) {
      val = (val===undefined ? 0 : val)
      next = (next===undefined ? null : next)
      return {val, next}
    }

    // 输入：l1 = [2,4,3], l2 = [5,6,4]
    // 输出：[7,0,8]
    // 解释：342 + 465 = 807.

    const addTwoNumbers = (l1, l2)=> {
      let temp = 0
      let cur = ListNode()
      const l3 = cur

      while(l1 || l2 || temp) {
        const val1 = l1 ? l1.val : 0
        const val2 = l2 ? l2.val : 0
        const sum = val1 + val2 + temp
        temp = Math.floor(sum / 10)
        const val = sum % 10
        cur.next = new ListNode(val)
        cur = cur.next
        if(l1) l1 = l1.next
        if(l2) l2 = l2.next
      }
      return l3.next
  }
  </script>
</body>
</html>